package com.micro.modual.service.impl;

import com.micro.common.MicroResponseCode;
import com.micro.common.util.JacksonUtil;
import com.micro.core.exception.BizException;
import com.micro.core.frame.service.impl.AbstractBasicsServiceImpl;
import com.micro.core.resp.RespJson;
import com.micro.core.resp.RespJsonData;
import com.micro.modual.entity.SystemElement;
import com.micro.modual.extension.SystemElementExtension;
import com.micro.modual.mapper.SystemElementMapper;
import com.micro.modual.mapper.SystemResourceAuthMapper;
import com.micro.modual.model.bo.systemelement.SystemElementBO;
import com.micro.modual.model.bo.systemelement.SystemElementValidBO;
import com.micro.modual.model.dto.systemelement.SystemElementDetailDTO;
import com.micro.modual.service.SystemElementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
*  <h1>系统元素表  实现</h1>
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020-08-04 00:11:10
 */
@Service("systemElementService")
public class SystemElementServiceImpl extends AbstractBasicsServiceImpl<SystemElement,Long> implements SystemElementService {

    @Autowired
    private SystemElementMapper systemElementMapper;

    @Autowired
    private SystemResourceAuthMapper systemResourceAuthMapper;
    
    /**
     * <h2>新增系统元素表详情</h2>
     * @param systemElementBO {@link SystemElementBO} 系统元素表 新增/修改 请求对象
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson addSystemElement(SystemElementBO systemElementBO) {
        SystemElement systemElement = SystemElementExtension.converAddSystemElement(systemElementBO);
        systemElementMapper.insertSelective(systemElement);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(), MicroResponseCode.SUCCESS.getResponseMessage());
    }

    /**
     * <h2>修改系统元素表详情</h2>
     * @param systemElementBO  {@link SystemElementBO} 系统元素表 新增/修改 请求对象
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson editSystemElement(SystemElementBO systemElementBO) {
        SystemElement systemElement = SystemElementExtension.converEditSystemElement(systemElementBO);
        systemElementMapper.updateByPrimaryKeySelective(systemElement);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

    /**
     * <h2>根据主键编号删除系统元素表信息</h2>
     * @param id  系统元素表 主键编号
     * @return  {@link RespJson} 返回结果基础类
     */

    @Override
    public RespJson deleteSystemElement(Long id) {
        SystemElement systemElement = systemElementMapper.selectByPrimaryKey(id);
        if(systemResourceAuthMapper.hasResourceDistribution(systemElement.getElementCode()) > 0){
            throw new BizException(MicroResponseCode.ELEMENT_HAS_DISTRIBUTION_NOT_ALLOW_DELETE.getResponseCode(),MicroResponseCode.ELEMENT_HAS_DISTRIBUTION_NOT_ALLOW_DELETE.getResponseMessage());
        }
        systemElementMapper.deleteByPrimaryKey(id);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

    @Override
    public RespJsonData<List<SystemElementDetailDTO>> findSystemElementList(String menuCode) {
        List<SystemElement> systemElementList =  systemElementMapper.findSystemElementList(menuCode);
        List<SystemElementDetailDTO> systemElementDetailDTOList = JacksonUtil.getList(JacksonUtil.toJson(systemElementList),SystemElementDetailDTO.class);
        return new RespJsonData<List<SystemElementDetailDTO>>(systemElementDetailDTOList, MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

    @Override
    public RespJson validataElementExist(SystemElementValidBO systemElementValidBO) {
        if(systemElementMapper.findElementExist(systemElementValidBO.getValidParam(),systemElementValidBO.getMenuCode()) > 0){
            throw new BizException(MicroResponseCode.ELEMENT_HAS_EXIST.getResponseCode(),MicroResponseCode.ELEMENT_HAS_EXIST.getResponseMessage());
        }else {
            return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
        }
    }

    @Override
    public RespJson validataElementAuthCodeExist(SystemElementValidBO systemElementValidBO) {
        if(systemElementMapper.findElementAuthCodeExist(systemElementValidBO.getValidParam(),systemElementValidBO.getMenuCode()) > 0){
            throw new BizException(MicroResponseCode.ELEMENT_HAS_EXIST.getResponseCode(),MicroResponseCode.ELEMENT_HAS_EXIST.getResponseMessage());
        }else {
            return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
        }
    }

}
